#include "mainwindow.h"

/******测试DTW使用，开始******/
#include "dbtools.h"
#include "dtwrecoge.h"
#include <QSqlDatabase>
#include <QSettings>
#include <iostream>
#include <QDebug>
#include <QMap>
/******测试DTW使用，结束******/

#include <QApplication>

#define TEST_DTW 0

int main(int argc, char *argv[])
{

#if !TEST_DTW

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();

#elif TEST_DTW

    /******测试DTW使用，开始******/
    QDateTime startTime1(QDate(2022,7,1), QTime(0,30,0));
    QDateTime endTime1(QDate(2022,7,1), QTime(1,0,0));
//    qDebug() << startTime;
    QDateTime startTime2(QDate(2022,7,1), QTime(1,30,0));
    QDateTime endTime2(QDate(2022,7,1), QTime(2,0,0));

    QDateTime startTime3(QDate(2022,7,1), QTime(2,30,0));
    QDateTime endTime3(QDate(2022,7,1), QTime(3,0,0));

    QSqlDatabase dbconnecton;

    QSettings setting("./config.ini", QSettings::IniFormat);
    QString sqlserverODBC = setting.value("SqlServer/ODBC").toString();
    QString sqlserverDATABASE = setting.value("SqlServer/DATABASE").toString();
    QString sqlserverUSERNAME = setting.value("SqlServer/USERNAME").toString();
//    QString exchangeTableNameInSqlServer = setting.value("SqlServer/EXCHANGETABLE").toString();

    /*
     * 在当前thread内，进行sql server 数据连接
     */
    dbconnecton = QSqlDatabase::addDatabase("QODBC", "thread_sql_server_connection");
    dbconnecton.setDatabaseName(sqlserverODBC + ";DATABASE="+sqlserverDATABASE);
    dbconnecton.setUserName(sqlserverUSERNAME);

    bool sqlserverOpenFlag = dbconnecton.open();
    if(sqlserverOpenFlag) {
        std::cout << "thread success connect sql server database..." << std::endl;
    } else {
        std::cout << "thread error connect sql server database..." << std::endl;
    }

    // 这里是获得的map，而不需要map
    QMap<QDateTime, double> result1 = DBTools::getUnderlineTagTimeSeris(dbconnecton, startTime1, endTime1, "C1", "AMT_DATA", "", "");
    QMap<QDateTime, double> result2 = DBTools::getUnderlineTagTimeSeris(dbconnecton, startTime2, endTime2, "C1", "AMT_DATA", "", "");
    QMap<QDateTime, double> result3 = DBTools::getUnderlineTagTimeSeris(dbconnecton, startTime3, endTime3, "C1", "AMT_DATA", "", "");

    dbconnecton.close();

    // 第一个是换转成list
    QList<double> list1 = result1.values();
    QList<double> list2 = result2.values();
    QList<double> list3 = result3.values();

    // 建立数组
    double *first = new double[result1.size()];
    double *second = new double[result2.size()];
    double *third = new double[result3.size()];

    // 遍历list给数组赋值
    for(int i=0;i<list1.size();++i) {
        first[i] = list1[i];
    }
    for(int i=0;i<list2.size();++i) {
        second[i] = list2[i];
    }
    for(int i=0;i<list3.size();++i) {
        third[i] = list3[i];
    }


    DTWRecoge *dtw = new DTWRecoge;

    std::cout << "start calculation" << std::endl;

    qDebug() << QDateTime::currentDateTime();
    double dist1 = dtw->DTWDistanceFun(first, result1.size(), second, result2.size(), 100);
    qDebug() << QDateTime::currentDateTime();
    double dist2 = dtw->DTWDistanceFun(third, result1.size(), second, result3.size(), 100);

    std::cout << "result is: " <<dist1 << " and " << dist2 << std::endl;

#endif

    /******测试DTW使用，结束******/

}
